package org.zfes.snowier.cloud.zbss.metting.biz.service.impl;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import javax.validation.constraints.NotNull;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.zfes.snowier.core.util.ZBeanUtil;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;

import org.zfes.snowier.core.util.ZAlert;
import org.zfes.snowier.core.util.ZAssert;
import org.zfes.snowier.cloud.zbss.metting.biz.service.IMettingsService;
import org.zfes.snowier.cloud.zbss.metting.biz.service.ISignupModelService;
import org.zfes.snowier.cloud.zbss.metting.biz.dao.SignupModelMapper;
import org.zfes.snowier.cloud.zbss.metting.biz.dto.SignupModelJsonDto;
import org.zfes.snowier.cloud.zbss.metting.biz.model.Mettings;
import org.zfes.snowier.cloud.zbss.metting.biz.model.SignupModel;
@Service
public class SignupModelServiceImpl implements ISignupModelService{
 	@Autowired
	private SignupModelMapper signupModelMapper;
 	@Autowired
	private IMettingsService mettingsService;
	@Transactional(rollbackFor=Exception.class)
 	@Override
	public void createSignupModel(Long metId,String formScript,String formJson) {
		ZAssert.notNull(metId, "请选择数据");
		ZAssert.hasText(formJson, "请编辑表单");
		ZAssert.hasText(formScript, "请编辑表单");
		if("[]".equals(formJson)) {
			ZAlert.serviceLogicalError("表单模板没有数据，请编辑后提交");
		}
		//状态_D#0待发布，1报名中，2报名结束，3会议进行中，4会议结束，5取消-1移除 #
		Optional<Mettings> metOp=mettingsService.loadMettingsById(metId);
		ZAssert.notEmpty(metOp, "未查询到会议信息");
		ZAssert.equals(Byte.valueOf("0"), metOp.get().getStatus(), "当前会议不是【待发布】状态，不允许再修改模板");
		
		
		Optional<SignupModel>	signupModelOP=loadSignupModelByMetId(metId);
		if(!signupModelOP.isPresent()) {
			
			SignupModel signupModel=new SignupModel(  metId,  formJson,  formScript) ;
			signupModel.setId(metId);
			ZBeanUtil.setCreateInfo(signupModel);
			ZBeanUtil.validateBean(signupModel);
			signupModelMapper.insertSelective(signupModel);
		}else {
			SignupModel signupModel=signupModelOP.get() ;
			signupModel.setFormScript(formScript);
			signupModel.setFormJson(formJson);
			ZBeanUtil.setUpdatedInfo(signupModel);
			ZBeanUtil.validateBean(signupModel);
			signupModelMapper.updateByPrimaryKeySelective(signupModel);
		}
		
	}
	
	
	
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public Optional<SignupModel> loadSignupModelById(Long id) {
		if(id!=null){
			return Optional.ofNullable(signupModelMapper.selectByPrimaryKey(id));
		}
		return Optional.empty();
	}
	
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public Optional<SignupModel> loadSignupModelByMetId(Long metId){
		if(metId!=null){
			return Optional.ofNullable(signupModelMapper.selectByMetId(metId));
		}
		return Optional.empty();
	}
	
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void deleteSignupModel(Long id) {
		ZAssert.notNull(id, "请选择要操作的数据");
		signupModelMapper.deleteByPrimaryKey(id);
	}



	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public List<SignupModelJsonDto> loadAmodelListByMetId(Long metId) {
		Optional<SignupModel> signupModelOp= loadSignupModelByMetId(metId);
		if(!signupModelOp.isPresent()) {
			return Lists.newArrayList();
		}
 		String formJson=signupModelOp.get().getFormJson();
 		
 		JSONArray formJsonArray = JSON.parseArray(formJson);
 		
 		List<SignupModelJsonDto> amodelList=formJsonArray.stream().map(obj -> {
 						JSONObject jsonObj = (JSONObject) obj; 
 					SignupModelJsonDto amodel= new SignupModelJsonDto(jsonObj.getString("type"),jsonObj.getString("name"),jsonObj.getString("label"))
 							.setSubtype(jsonObj.getString("subtype"));
 					JSONArray values=jsonObj.getJSONArray("values");
 					//checkbox 等多选的label-value
 					if(values!=null&&!values.isEmpty()) {
 						Map<String, String> valuesOpMap=  values.stream()
 								.collect(Collectors.toMap(ojbx-> ((JSONObject) ojbx).getString("value"), ojbx-> ((JSONObject) ojbx).getString("label")));
 						amodel.setValues(valuesOpMap);
 					}
					return amodel;
 					}).collect(Collectors.toList());
 		return amodelList;
	}

	
}